<template>
	<view>
		<u-navbar>
			<view class="u-nav-slot" slot="left" :style="{width:`calc(750rpx - 40rpx - ${paddingright})`}">
				<image class="back_icon" @click="back" src="/static/img/back.png" mode=""></image>
				<view class="left">
					<u-search placeholder="搜索你需要购买的商品" :disabled='false' searchIcon=' ' height="73rpx" :focus='true'
						v-model="keyword" :showAction='false' @change="change" :clearabled='true'></u-search>
				</view>
			</view>
		</u-navbar>
		<view class="searchlist" :style="{paddingTop:marginTop+'px'}">
			<view class="searchitem" v-for="(item,index) in list" :key="index" @click="clickto(item)">
				<image class="sousuo_icon" src="/static/img/sousuo.png" mode=""></image>
				<rich-text class="itemcon" :nodes='brightenKeyword(item.name,keyword)'></rich-text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		search
	} from '@/api/product.js'
	export default {
		data() {
			return {
				keyword: '',
				list: [],
				paddingright: 0,
				marginTop: 0,
				type: ''
			}
		},
		methods: {
			back() {
				uni.navigateBack()
			},
			brightenKeyword(val, keyword) {
				const Reg = new RegExp(keyword, 'i');
				if (val) {
					return val.replace(Reg, `<span style="color: #FB651A;">${keyword}</span>`);
				}
			},
			change(e) {
				// console.log(e)
				search({
					keyword: e,
					type: this.type
				}).then(res => {
					// console.log(res)
					this.list = res.data.data
				})
			},
			clickto(item) {
				uni.$u.route('/pages/product/prodetail', {
					id: item.id,
					title: '商品详情'
				})
			}
		},
		onLoad(option) {
			// this.type=option.type
			if (option.type == 'tz') this.type = 'tz'
			if (option.type == 'jf') this.type = 'jf'
			// console.log(option.type)
			if (!option.type) this.type = 'no'
			this.paddingright = uni.getStorageSync('paddingright')
			this.marginTop = 53 + uni.getStorageSync('statusBarHeight')
		},
	}
</script>

<style lang="less">
	.searchitem {
		display: flex;
		align-items: center;
		padding: 30rpx 31rpx;

		.sousuo_icon {
			width: 34rpx;
			height: 36rpx;
			margin-right: 11rpx;
		}

		.itemcon {
			color: #333;
			font-size: 30rpx;
		}
	}

	.u-nav-slot {
		display: flex;
		align-items: center;
		width: calc(750rpx - 40rpx);

		.back_icon {
			width: 18rpx;
			height: 32rpx;
			margin-right: 26rpx;
		}

		.left {
			width: 100%;
		}
	}
</style>